import type { CommonState } from '../common/commonApi'
import { useApi } from '@/composables/useApi'

export const AppMenuTypes = [
  { label: '菜单', value: 'MENU', tagType: 'primary' },
  { label: '功能', value: 'FUNCTION', tagType: 'info' },
] as const
export type AppMenuType = typeof AppMenuTypes[number]['value']

/**
 * 系统菜单
 */
export interface AppMenu {
  /** ID */
  id?: number
  /** 应用类型 */
  appType: string
  /** 上级ID */
  parentId?: number
  /** 模块名称 */
  moduleName?: string
  /** 资源名称 */
  name?: string
  /** 资源编码 */
  code?: string
  /** 资源类型 */
  type: AppMenuType
  /** 资源状态 */
  state: CommonState
  /** 资源优先级 */
  priority?: number
  /** 资源页面路径 */
  path?: string
  /** 资源图标 */
  icon?: string
  /** 是否可见 */
  visible?: boolean
  /** API集合 */
  apis?: string[]
  /** 子级资源 */
  children?: AppMenu[]
}

const basePath = '/system/app/menu'
export const appMenuApi = {
  queryTree: (appType: string) => useApi<null, AppMenu[]>(`${basePath}/tree?appType=${appType}`).get(),
  findDetail: async (id: number) => useApi<any, AppMenu>(`${basePath}/${id}`).get(),
  create: useApi<AppMenu, AppMenu>(`${basePath}`).post,
  modify: useApi<AppMenu, AppMenu>(`${basePath}`).patch,
  remove: async (id: number) => useApi<null, AppMenu>(`${basePath}/${id}`).remove(),
}
